package com.magictower.primary.service;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.format.Formatter;
import android.widget.RemoteViews;

import com.magictower.primary.R;
import com.magictower.primary.receiver.MyAppWidget;
import com.magictower.primary.utils.SystemInfoUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author kejunzhan
 * @since 2017/12/18
 */

public class KillProcessWidgetService extends Service {

    private AppWidgetManager widgetManager;
    private Timer timer;
    private TimerTask task;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //桌面控件管理者
        widgetManager = AppWidgetManager.getInstance(this);
        /**
         * 每5秒更新定时器
         * 1.初始化定时器
         * 2.初始化一个任务
         * 3.从0开始，每5秒更新一次
         */
        timer = new Timer();
        task =   new TimerTask() {
            @Override
            public void run() {
                /*
                 * 初始化一个远程view
                 * Remote远程
                 */
                RemoteViews view = new RemoteViews(getPackageName(), R.layout.process_widget);
                /*
                 * 需要注意，这里面有findviewbyid这个方法
                 * 设置当前有几个进程
                 * 设置可用内存
                 */
                int processCount = SystemInfoUtils.getProcessCount(getApplicationContext());
                view.setTextViewText(R.id.process_count, "正在运行的软件：" + processCount);
                long availMen = SystemInfoUtils.getAvailMem(getApplicationContext());
                view.setTextViewText(R.id.process_memory, "可用内存：" + Formatter.formatFileSize(getApplicationContext(), availMen));

                //发送一个隐式意图
                Intent intent = new Intent();
                intent.setAction("com.magictower.primary");

                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
                view.setOnClickPendingIntent(R.id.btn_clear, pendingIntent);
                /*
                第一个参数表示上下文
                第二个参数表示当前哪一个广播去处理的桌面小控件
                 */
                ComponentName provider = new ComponentName(getApplication(), MyAppWidget.class);
                //更新桌面
                widgetManager.updateAppWidget(provider, view);
                System.out.println("start KillProcessWidgetService");
            }
        };
        timer.schedule(task, 0, 5000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (task !=null) {
            task.cancel();
            task = null;
        }
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        System.out.println("stop KillProcessWidgetService");
    }
}
